//
//  W_DetailViewController.m
//  Go!
//
//  Created by 凌風 on 16/7/13.
//  Copyright © 2016年 凌风. All rights reserved.
//

#import "W_DetailViewController.h"
#import "W_DealTableViewCell.h"
#import "UIImageView+ImageWithURL.h"
#import "WHandleData.h"
#import "W_DealModel.h"
#import "HQYHomeCellDetailViewController.h"
#import "W_MapViewController.h"

#define DealCellID @"dealCellId"

@interface W_DetailViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIImageView *bgImageView;/**< 背景图片 */
@property (nonatomic, strong) UIImageView *shopImageView; /**< 商铺图片 */
@property (nonatomic, strong) UILabel_Font *shopNameLabel; /**< 商铺名称 */
@property (nonatomic, strong) UILabel_Font *shopAddressLabel; /**< 店铺地址 */
@property (nonatomic, strong) UIView *hLine; /**< 分界线 */
@property (nonatomic, strong) UILabel_Font *openTimeLabel; /**< 开放时间 */
@property (nonatomic, strong) UILabel_Font *phoneLabel; /**< 联系电话 */
@property (nonatomic, strong) UITableView *dealTableView; /**< 团单列表 */

#pragma mark -- 数据源
@property (nonatomic, strong) WHandleData *handleData; /**< 网络请求数据处理 */
@property (nonatomic, strong) NSArray *dealsDataSource; /**< 团单列表数据数组 */

- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */
@end

@implementation W_DetailViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    self.navigationController.navigationBarHidden = false;
    self.navigationController.navigationBar.hidden = false;
    self.tabBarController.tabBar.hidden = true;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.tabBarController.tabBar.hidden = false;
}

- (void)initializeDataSource {
    self.navigationItem.title = self.detailInfo[@"shop_name"];
    NSLog(@"%@", self.detailInfo);
//    NSLog(@"%@", self.imageURL);
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondsToNotification:) name:@"DetailVcShopDeals" object:nil];
    
    // 获取商户下团单列表
    [self.handleData getShopDealsWithShopID:self.detailInfo[@"shop_id"]];
}

- (void)initializeUserInterface {
    
    // 添加背景视图
    [self.view addSubview:self.bgImageView];
    [self.bgImageView setImageWithURL:self.imageURL placeHolderImage:[UIImage imageNamed:@"展览馆.png"]];
    
    // 添加商铺图片视图
    [self.bgImageView addSubview:self.shopImageView];
    
    [self.shopImageView setImageWithURL:self.imageURL placeHolderImage:[UIImage imageNamed:@"展览馆.png"]];
    
    // 添加商铺名称
    [self.bgImageView addSubview:self.shopNameLabel];
    self.shopNameLabel.text = self.detailInfo[@"shop_name"];
    [self.shopNameLabel sizeToFit];
    
    // 添加商铺地址
    [self.view addSubview:self.shopAddressLabel];
    self.shopAddressLabel.text = self.detailInfo[@"address"];
    [self.shopAddressLabel sizeToFit];
    
    // 添加垂直分割线
    UIView *vLine = [[UIView alloc] initWithFrame:CGRectMake(Size_W(340), CGRectGetMaxY(self.bgImageView.frame) + Size_H(8), 1, CGRectGetHeight(self.shopAddressLabel.bounds)  + Size_H(20))];
    vLine.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:vLine];
    
    // 添加地图按钮
    UIButton *mapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    mapBtn.frame = CGRectMake(CGRectGetMaxX(vLine.frame) + Size_W(10), CGRectGetMinY(vLine.frame) + (CGRectGetHeight(vLine.bounds) - (ScreenW - CGRectGetMaxX(vLine.frame) - Size_W(20))) / 2.0 , ScreenW - CGRectGetMaxX(vLine.frame) - Size_W(20) , ScreenW - CGRectGetMaxX(vLine.frame) - Size_W(20));
    [mapBtn setImage:[UIImage imageNamed:@"地图.png"] forState:UIControlStateNormal];
    
    [mapBtn addTarget:self action:@selector(respondsToMapBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:mapBtn];
    
    // 添加水平分割线
    self.hLine = [[UIView alloc] initWithFrame:CGRectMake(Size_W(8), CGRectGetMaxY(vLine.frame) + Size_H(8), ScreenW - Size_W(8), 1)];
    self.hLine.backgroundColor = [UIColor lightGrayColor];
    
    [self.view addSubview:self.hLine];
    
    // 添加‘开放时间’标签
    self.openTimeLabel.text = self.detailInfo[@"open_time"];
    [self.view addSubview:self.openTimeLabel];
    [self.openTimeLabel sizeToFit];
    
    // 添加‘联系电话’标签
    self.phoneLabel.text = self.detailInfo[@"phone"];
    [self.view addSubview:self.phoneLabel];
    [self.phoneLabel sizeToFit];
    
}


#pragma mark -- <UITableViewDataSource, UITableViewDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dealsDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    W_DealTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DealCellID];
    cell.model = self.dealsDataSource[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    W_DealModel *model = self.dealsDataSource[indexPath.row];
    
    CGFloat height = [model heightForDescriptionString] + Size_H(90);

    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
    HQYHomeCellDetailViewController *detailVc = [[HQYHomeCellDetailViewController alloc] init];
    
    W_DealModel *model = self.dealsDataSource[indexPath.row];
    detailVc.url = model.deal_url;
    
    [self.navigationController pushViewController:detailVc animated:true];
    
}

#pragma mark -- 监听事件的回调方法
- (void)respondsToNotification:(NSNotification *)notif {
    if ([notif.userInfo[@"user"] isEqualToString:@"shopDeals"]) {
        if ([notif.object isKindOfClass:[NSArray class]]) {
            self.dealsDataSource = notif.object;
            
            // 添加团单列表
            [self.view addSubview:self.dealTableView];
            [self.dealTableView reloadData];
        }
    }
}


- (void)respondsToMapBtn:(UIButton *)sender {
    W_MapViewController *mapVc = [[W_MapViewController alloc] init];
    
    mapVc.longitude = [self.detailInfo[@"longitude"] floatValue];
    mapVc.latitude = [self.detailInfo[@"latitude"] floatValue];
    mapVc.shopName = self.detailInfo[@"shop_name"];
    mapVc.shopAddress = self.detailInfo[@"address"];
    
    [self.navigationController pushViewController:mapVc animated:true];
}

#pragma mark -- Setter
- (void)setDetailInfo:(NSMutableDictionary *)detailInfo {
    if (_detailInfo != detailInfo) {
        _detailInfo = detailInfo;
    }
    
    [self initializeDataSource];
    [self initializeUserInterface];
}

#pragma mark -- Getter
- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, ScreenW, Size_H(100))];
        
        UIView *maskView = [[UIView alloc] initWithFrame:_bgImageView.bounds];
        
        maskView.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.8];
        
        [_bgImageView addSubview:maskView];
        
    }
    
    return _bgImageView;
}

- (UIImageView *)shopImageView {
    if (!_shopImageView) {
        _shopImageView = [[UIImageView alloc] initWithFrame:CGRectMake(Size_W(8), Size_H(8), Size_W(100), CGRectGetHeight(self.bgImageView.bounds) - Size_W(8) * 2)];
    }
    
    return _shopImageView;
}

- (UILabel_Font *)shopNameLabel {
    if (!_shopNameLabel) {
        _shopNameLabel = [[UILabel_Font alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.shopImageView.frame) + Size_W(8), CGRectGetMinY(self.shopImageView.frame), ScreenW - CGRectGetMaxX(self.shopImageView.frame) - Size_W(16), Size_H(40))];
        _shopNameLabel.font = [UIFont boldSystemFontOfSize:Font(20)];
        _shopNameLabel.textColor = [UIColor whiteColor];
    }
    
    return _shopNameLabel;
}

- (UILabel_Font *)shopAddressLabel {
    if (!_shopAddressLabel) {
        
        UILabel_Font *label = [[UILabel_Font alloc] initWithFrame:CGRectMake(Size_W(8), CGRectGetMaxY(self.bgImageView.frame) + Size_H(18), Size_W(10), Size_H(40))];
        label.text = @"地址:";
        
        [self.view addSubview:label];
        [label sizeToFit];
        
        _shopAddressLabel = [[UILabel_Font alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label.frame) + 2, CGRectGetMinY(label.frame), Size_W(300), Size_H(100))];
        _shopAddressLabel.font = [UIFont systemFontOfSize:Font(17)];
        
        _shopAddressLabel.numberOfLines = 0;
    }
    
    return _shopAddressLabel;
}

- (UILabel_Font *)openTimeLabel {
    if (!_openTimeLabel) {
        UILabel_Font *label = [[UILabel_Font alloc] initWithFrame:CGRectMake(Size_W(8), CGRectGetMaxY(self.hLine.frame) + Size_H(10), Size_W(10), Size_H(40))];
        label.text = @"营业时间:";
        
        [self.view addSubview:label];
        [label sizeToFit];
        
        _openTimeLabel = [[UILabel_Font alloc] initWithFrame:CGRectMake(Size_W(38), CGRectGetMaxY(label.frame) + Size_H(5), ScreenW - Size_W(16), Size_H(40))];
        
    }
    
    return _openTimeLabel;
}

- (UILabel_Font *)phoneLabel {
    if (!_phoneLabel) {
        UILabel_Font *label = [[UILabel_Font alloc] initWithFrame:CGRectMake(Size_W(8), CGRectGetMaxY(self.openTimeLabel.frame) + Size_H(10), Size_W(10), Size_H(40))];
        label.text = @"联系电话:";
        
        [self.view addSubview:label];
        [label sizeToFit];
        
        _phoneLabel = [[UILabel_Font alloc] initWithFrame:CGRectMake(Size_W(38), CGRectGetMaxY(label.frame) + Size_H(5), ScreenW - Size_W(16), Size_H(40))];
    }
    
    return _phoneLabel;
}

- (UITableView *)dealTableView {
    if (!_dealTableView) {
        
        UILabel_Font *headerLabel = [[UILabel_Font alloc] initWithFrame:CGRectMake(-1, CGRectGetMaxY(self.phoneLabel.frame), ScreenW + 2, Size_H(20))];
        headerLabel.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.8];

        headerLabel.textColor = [UIColor blackColor];
        
        NSString *str = @"旅游线路:";
        NSInteger length = [str length];
        
        NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:str];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
        style.firstLineHeadIndent = 8;
        
        [attriString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, length)];
        headerLabel.attributedText = attriString;
        [self.view addSubview:headerLabel];
        
        _dealTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(headerLabel.frame), ScreenW, ScreenH - CGRectGetMaxY(headerLabel.frame)) style:UITableViewStylePlain];
        
        _dealTableView.dataSource = self;
        _dealTableView.delegate = self;
        
        _dealTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
//        _dealTableView.estimatedRowHeight = Size_H(90);
        
        [_dealTableView registerClass:[W_DealTableViewCell class] forCellReuseIdentifier:DealCellID];
    }
    
    return _dealTableView;
}

- (WHandleData *)handleData {
    if (!_handleData) {
        _handleData = [[WHandleData alloc] init];
    }
    
    return _handleData;
}

@end
